/**
 * @file lv_port_disp_templ.c
 *
 */

/*Copy this file as "lv_port_disp.c" and set this value to "1" to enable content*/
#if 1

/*********************
 *      INCLUDES
 *********************/
#include "lv_port_disp_template.h"
#include <stdbool.h>
#include "public.h"

#include "ltdc.h"
#include "dma2d.h"

#define MY_DISP_HOR_RES    480
#define MY_DISP_VER_RES    480
/*********************
 *      DEFINES
 *********************/
#ifndef MY_DISP_HOR_RES
    #warning Please define or replace the macro MY_DISP_HOR_RES with the actual screen width, default value 320 is used for now.
    #define MY_DISP_HOR_RES    320
#endif

#ifndef MY_DISP_VER_RES
    #warning Please define or replace the macro MY_DISP_VER_RES with the actual screen height, default value 240 is used for now.
    #define MY_DISP_VER_RES    240
#endif

#define BYTE_PER_PIXEL (LV_COLOR_FORMAT_GET_SIZE(LV_COLOR_FORMAT_RGB565)) /*will be 2 for RGB565 */

/**********************
 *      TYPEDEFS
 **********************/
uint32_t FrameBuffer=0xC0000000;
lv_display_t * disp;
volatile bool FirstFrameReady = false;
volatile bool SecondFrameReady = false;

uint8_t* buf_1=(uint8_t*)((uint32_t)0xC0000000);
uint8_t* buf_2=(uint8_t*)((uint32_t)0xC0100000);
uint8_t* buf_3=(uint8_t*)((uint32_t)0xC0200000);
uint8_t  sram_buf1[150*1024] __attribute__((section(".ARM.__at_0x24000000")));
uint8_t  sram_buf2[150*1024] __attribute__((section(".ARM.__at_0x24025800")));
/**********************
 *  STATIC PROTOTYPES
 **********************/
static void disp_init(void);

static void disp_flush(lv_display_t * disp, const lv_area_t * area, uint8_t * px_map);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/
static void disp_flush_complete (DMA2D_HandleTypeDef *hdma2d)
{
  lv_display_flush_ready(lv_display_get_default());
}

void lv_port_disp_init(void)
{
    /*-------------------------
     * Initialize your display
     * -----------------------*/
    disp_init();

	disp = lv_display_create(MY_DISP_HOR_RES, MY_DISP_VER_RES);
    lv_display_set_flush_cb(disp, disp_flush);
//	 LV_ATTRIBUTE_MEM_ALIGN
//    static uint8_t buf_2_1[MY_DISP_HOR_RES * 160 * BYTE_PER_PIXEL] __attribute__((section(".bss.ARM.__at_0x24000000")));

//    LV_ATTRIBUTE_MEM_ALIGN
//    static uint8_t buf_2_2[MY_DISP_HOR_RES * 160 * BYTE_PER_PIXEL] __attribute__((section(".bss.ARM.__at_0x2403E800")));
//    lv_display_set_buffers(disp, buf_2_1, buf_2_2, 256000, LV_DISPLAY_RENDER_MODE_PARTIAL);
   
    lv_display_set_buffers(disp, sram_buf1, sram_buf2, 150*1024, LV_DISPLAY_RENDER_MODE_PARTIAL);
    /*------------------------------------
     * Create a display and set a flush_cb
     * -----------------------------------*/
//    lv_display_t * disp = lv_display_create(MY_DISP_HOR_RES, MY_DISP_VER_RES);
//    lv_display_set_flush_cb(disp, disp_flush);
//	lv_display_set_antialiasing(disp, true);//抗锯齿
//    /* Example 1
//     * One buffer for partial rendering*/

//	lv_display_set_buffers(disp, buf_1, buf_2, (MY_DISP_HOR_RES*MY_DISP_VER_RES*BYTE_PER_PIXEL), LV_DISPLAY_RENDER_MODE_DIRECT);	
/**
 * Create a direct render mode display bound to a LTDC layer.
 * @param fb_adr_1      The LTDC layer's framebuffer memory address.
 * @param fb_adr_2      An additional framebuffer-sized buffer to use for double buffering, or `NULL`.
 * @param layer_idx     The LTDC layer number to bind the display to. Typically 0 or 1.
 * @return              The display.
 */
//	lv_st_ltdc_create_direct(buf_1,buf_2,0);

/**
 * Create a partial render mode display bound to a LTDC layer. The layer's framebuffer is flushed to internally.
 * Enable `LV_ST_LTDC_USE_DMA2D_FLUSH` for parallel flushing.
 * @param render_buf_1  A render buffer.
 * @param render_buf_2  An additional render buffer for double-buffering, or `NULL`.
 * @param buf_size      The size of the buffer(s) in bytes.
 * @param layer_idx     The LTDC layer number to bind the display to. Typically 0 or 1.
 * @return              The display.
 */
//	lv_st_ltdc_create_partial(sram_buf1,sram_buf2,120*1024,0);


#if 0
    /* Example 2
     * Two buffers for partial rendering
     * In flush_cb DMA or similar hardware should be used to update the display in the background.*/
    LV_ATTRIBUTE_MEM_ALIGN
    static uint8_t buf_2_1[MY_DISP_HOR_RES * 10 * BYTE_PER_PIXEL];

    LV_ATTRIBUTE_MEM_ALIGN
    static uint8_t buf_2_2[MY_DISP_HOR_RES * 10 * BYTE_PER_PIXEL];
    lv_display_set_buffers(disp, buf_2_1, buf_2_2, sizeof(buf_2_1), LV_DISPLAY_RENDER_MODE_PARTIAL);

    /* Example 3
     * Two buffers screen sized buffer for double buffering.
     * Both LV_DISPLAY_RENDER_MODE_DIRECT and LV_DISPLAY_RENDER_MODE_FULL works, see their comments*/
    LV_ATTRIBUTE_MEM_ALIGN
    static uint8_t buf_3_1[MY_DISP_HOR_RES * MY_DISP_VER_RES * BYTE_PER_PIXEL];

    LV_ATTRIBUTE_MEM_ALIGN
    static uint8_t buf_3_2[MY_DISP_HOR_RES * MY_DISP_VER_RES * BYTE_PER_PIXEL];
    lv_display_set_buffers(disp, buf_3_1, buf_3_2, sizeof(buf_3_1), LV_DISPLAY_RENDER_MODE_DIRECT);
#endif
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

/*Initialize your display and the required peripherals.*/
static void disp_init(void)
{
    /*You code here*/
}

volatile bool disp_flush_enabled = true;

/* Enable updating the screen (the flushing process) when disp_flush() is called by LVGL
 */
void disp_enable_update(void)
{
    disp_flush_enabled = true;
}

/* Disable updating the screen (the flushing process) when disp_flush() is called by LVGL
 */
void disp_disable_update(void)
{
    disp_flush_enabled = false;
}
void HAL_LTDC_LineEvenCallback(LTDC_HandleTypeDef *hltdc)
{   
	//重新载入参数，新显存地址生效，此时显示才会更新
	//每次进入币断才会更新显示，这样能有效避免撕裂现象
	__HAL_LTDC_RELOAD_CONFIG(hltdc);			
	HAL_LTDC_ProgramLineEvent(hltdc, 1);//重新设置中断
}
/*Flush the content of the internal buffer the specific area on the display.
 *`px_map` contains the rendered image as raw pixel map and it should be copied to `area` on the display.
 *You can use DMA or any hardware acceleration to do this operation in the background but
 *'lv_display_flush_ready()' has to be called when it's finished.*/
static void disp_flush(lv_display_t * disp_drv, const lv_area_t * area, uint8_t * px_map)
{
	if(disp_flush_enabled) {
        /*The most simple case (but also the slowest) to put all pixels to the screen one-by-one*/


		ltdc_color_fill(area->x1,area->y1,area->x2,area->y2,(uint16_t*)px_map,FrameBuffer);



    }
	

	if(lv_disp_flush_is_last(disp))
		{	
			if(FrameBuffer == 0xC0000000)
			{
				FirstFrameReady = true;
				LTDC_Layer1->CFBAR = FrameBuffer;	
				FrameBuffer = 0xC00BB800;
			}
			else if(FrameBuffer == 0xC00BB800)
			{
				SecondFrameReady = true;
				LTDC_Layer1->CFBAR = FrameBuffer;	
				FrameBuffer = 0xC0000000;
			}
			

		}


		
//    if(disp_flush_enabled) 
//	{	
//	
//        /*The most simple case (but also the slowest) to put all pixels to the screen one-by-one*/
////		SCB_CleanInvalidateDCache();

//		if(lv_display_flush_is_last(disp_drv))
//		{
//			
//			 while (!(LTDC->CDSR & LTDC_CDSR_VSYNCS));
//			//memcpy(buf_1, buf_2, (MY_DISP_HOR_RES*MY_DISP_VER_RES*BYTE_PER_PIXEL));
//			HAL_LTDC_SetAddress(&hltdc, (uint32_t)(lv_display_get_buf_active(lv_display_get_default())->data), LTDC_LAYER_1);
//			
//		}
//       
//    }

//	
//    /*IMPORTANT!!!
//     *Inform the graphics library that you are ready with the flushing*/
//    lv_display_flush_ready(disp_drv);
}

#else /*Enable this file at the top*/

/*This dummy typedef exists purely to silence -Wpedantic.*/
typedef int keep_pedantic_happy;
#endif
